package com.hbyj.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.hbyj.db.factory.CDBConnection;

public class CopyTblTool {

	public  Connection connSource ;
	public  Connection connTarget;
	
	public  Connection getConnSource() {
		return connSource;
	}
	public  void setConnSource(Connection connSource) {
		this.connSource = connSource;
	}
	public  Connection getConnTarget() {
		return connTarget;
	}
	public  void setConnTarget(Connection connTarget) {
		this.connTarget = connTarget;
	}
	public  boolean convertData(String sourceTblName,String targetTblName){
		boolean res = false;
		Statement statSQL = null;
		Statement statDm = null;
		ResultSet rst=null;
		try {
			if(connSource==null||connTarget==null){
				System.out.println("数据库连接未配置");
				return res;
			}
			statSQL=connSource.createStatement();
			rst=statSQL.executeQuery("select * from "+sourceTblName);
			deleteTargetData(targetTblName);
			//取得数据表中的字段数目，类型等返回结果
	        ResultSetMetaData rsmd = rst.getMetaData();
			int columnCount = rst.getMetaData().getColumnCount(); //列的总数
			String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
			while(rst.next()){	
				String str_result="'";
				for(int i=1;i<columnCount+1;i++){
					/*if(rst.getMetaData().getColumnName(i).equals("TIMEFLAG")){
						str_result += date+"','";
					}else*/
					str_result += rst.getObject(i)+"','";
				}
				if(!str_result.equals("'")){
					str_result = str_result.substring(0, str_result.length()-2);
					str_result = str_result.replaceAll("null", "").replaceAll("NULL", "");
					
					statDm = connTarget.createStatement();
					statDm.executeUpdate("INSERT INTO "+targetTblName+" VALUES ("+str_result+")");
					// System.out.println(t);
					 //statDm.executeUpdate("INSERT INTO HBEGS_DZZH.HBEGS_DZZH.DZZH_JCYJYJXX VALUES ('588','1','1','1','1','2010-09-18 15:24:14','1')");
				}
			}
			System.out.println("数据拷贝成功：表"+targetTblName);
			res = true;
		} catch (Exception e) {
			System.out.println(e);
			System.out.println("数据拷贝错误：表"+targetTblName);
		} finally {
			try {//关闭数据库
				rst.close();
				statSQL.close();
				if(statDm!=null)
					statDm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}		
		}
		return res;
	}
	/**
	 * author cuiyi
	 * time 2012-9-11
	 * disc 删除目标表的所有数据
	 * @param tableName
	 * @return
	 */
	private  int deleteTargetData(String tableName){
		Statement statDm = null;
		int delFlg = 0;
		try{
			statDm = connTarget.createStatement();
			int t= statDm.executeUpdate("DELETE FROM "+tableName);
		}catch (Exception e) {
			System.out.println("删除表"+tableName+"失败");
		}finally{
			try {//关闭数据库
				statDm.close();
			} catch (SQLException e) {
				System.out.println("statement关闭错误");
			}	
		}
		
		return delFlg;
	}
}
